class ListNode {
  constructor(val) {
    this.next = null;
    this.val = val;
  }
}

class List {
  constructor() {
    this.head = null;
    this.length = 0;
  }

  static createNode(val) {
    return new ListNode(val);
  }

  // 往头部插入数据
  insert(node) {
    // 如果head后面有指向的节点
    if (this.head) {
      node.next = this.head;
    } else {
      node.next = null;
    }
    this.head = node;
    this.length++;
  }

  find(val) {
    let node = this.head;
    while (node !== null && node.val !== val) {
      node = node.next;
    }
    return node;
  }

  delete(node) {
    if (this.length === 0) {
      throw 'node is undefined';
    }

    if (node === this.head) {
      this.head = node.next;
      this.length--;
      return;
    }

    let prevNode = this.head;

    while (prevNode.next !== node) {
      prevNode = prevNode.next;
    }

    if (node.next === null) {
      prevNode.next = null;
    }
    if (node.next) {
      prevNode.next = node.next;
    }
    this.length--;
  }
}
// let a = new ListNode(-1);
// const l1 = [1,2,4], l2 = [1,3,4];
// console.log(a, 'sss');
// console.log(new ListNode(l1), new ListNode(l2));
module.exports = ListNode;